﻿using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace FrameWork
{
    partial class RsaHelp
    {
        public static string DecryptString(byte[] data, string publicKey)
        {
            var paraPub = RsaFromPkcs8.ConvertFromPublicKey(publicKey);
            var rsaPub = new RSACryptoServiceProvider();
            rsaPub.ImportParameters(paraPub);
            var parms = rsaPub.ExportParameters(false);
            var e = new BigInteger(parms.Exponent);
            var n = new BigInteger(parms.Modulus);
            var encryption = Encoding.UTF8.GetString(data);
            var decryption = DecryptString(encryption, e, n);
            return decryption;
        }
        private static string DecryptString(string encryptString, BigInteger e, BigInteger n)
        {
            var result = new StringBuilder();
            var strarr1 = encryptString.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var temp in from block in strarr1 select new BigInteger(block, 16) into biText select biText.ModPow(e, n) into biEnText select Encoding.UTF8.GetString(biEnText.GetBytes()))
            {
                result.Append(temp);
            }
            return result.ToString();
        }
    }
}
